package cn.pping.org.controller;

import cn.pping.basic.annotation.PreAuthorize;
import cn.pping.basic.util.JsonResult;
import cn.pping.basic.util.PageList;
import cn.pping.org.domain.Department;
import cn.pping.org.query.DepartmentQuery;
import cn.pping.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    public IDepartmentService iDepartmentService;

    /**
     * @Title: loadAll 查询全部
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 9:37
     * @Parameters: []
     * @Return java.util.List<cn.pping.org.domain.Departmant>
     */

    @GetMapping
    @PreAuthorize(name = "部门查询全部",sn = "dept:get")
    public List<Department> loadAll() {
        return iDepartmentService.loadAll();
    }

    /**
     * @Title: loadById 根据ID查询
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 9:37
     * @Parameters: [id]
     * @Return cn.pping.org.domain.Departmant
     */

    @GetMapping("/{id}")
    @PreAuthorize(name = "部门根据id查询",sn = "dept:get")
    public Department loadById(@PathVariable("id") Long id) {
        return iDepartmentService.loadById(id);
    }


    /**
     * @Title: romove 根据Id删除
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 10:26
     * @Parameters: [id]
     * @Return cn.pping.basic.util.JsonResult
     */

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "部门根据id删除",sn = "dept:delete")
    public JsonResult remove(@PathVariable("id") Long id) {

        try {
            iDepartmentService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");

        }
    }

    /**
     * @Title: batchRomove 批量删除
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/19 19:21
     * @Parameters: [ids]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PatchMapping
    @PreAuthorize(name = "部门根据批量删除",sn = "dept:patch")
    public JsonResult batchRomove(@RequestBody List<Long> ids) {
        try {
            iDepartmentService.batchRomove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");

        }
    }

    /**
     * @Title: addOrUpdate 添加修改
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 10:28
     * @Parameters: [departmant]
     * @Return cn.pping.basic.util.JsonResult
     */
    @PutMapping
    @PreAuthorize(name = "部门新增修改",sn = "dept:put")
    public JsonResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                iDepartmentService.add(department);
            } else {
                iDepartmentService.update(department);
            }
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * @Title: queryPage 分页
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 10:34
     * @Parameters: [id]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PostMapping
    @PreAuthorize(name = "部门分页",sn = "dept:post")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query) {
        try {
            return iDepartmentService.pageQuery(query);
        } catch (Exception e) {
            // 应为可能会报错所以前端页面返回一个空的参数 这样前端就显示数值
            return new PageList<>();
        }

    }

    /**
     * @Title: deptTree 获取级联数据
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/20 10:54
     * @Parameters: []
     * @Return java.util.List<cn.pping.org.domain.Department>
     */

    @GetMapping("/deptTree")
    @PreAuthorize(name = "部门管理数",sn = "dept:deptTree") //value后面用来做权限校验
    public List<Department> deptTree() {
        return iDepartmentService.deptTree();
    }

}
